package com.originskyseed.platform.common.api.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Author: wlj @date: 2021/3/30
 */

public final class DataUtil {

	/**
	 * 英文简写（默认）如：2010-12-01
	 */
	public static String FORMAT_SHORT = "yyyy-MM-dd";
	/**
	 * 英文全称 如：2010-12-01 23:15:06
	 */
	public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
	/**
	 * 精确到毫秒的完整时间 如：yyyy-MM-dd HH:mm:ss.S
	 */
	public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
	/**
	 * 中文简写 如：2010年12月01日
	 */
	public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
	/**
	 * 中文全称 如：2010年12月01日 23时15分06秒
	 */
	public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
	/**
	 * 精确到毫秒的完整中文时间
	 */
	public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";

	/**
	 * 获得默认的 date pattern String
	 */
	public static String getDatePattern() {
		return FORMAT_LONG;
	}

	/**
	 * 根据预设格式返回当前日期 String
	 */
	public static String getNow() {
		return format(new Date());
	}

	/**
	 * 根据用户格式返回当前日期 String
	 */
	public static String getNow(String format) {
		return format(new Date(), format);
	}

	/**
	 * 使用预设格式格式化日期
	 *
	 * @param date String
	 */
	public static String format(Date date) {
		return format(date, getDatePattern());
	}

	/**
	 * 使用用户格式格式化日期
	 *
	 * @param date    日期
	 * @param pattern 日期格式 String
	 */
	public static String format(Date date, String pattern) {
		String returnValue = "";
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

	/**
	 * 使用预设格式提取字符串日期
	 *
	 * @param strDate 日期字符串 Date
	 */
	public static Date parse(String strDate) throws ParseException {
		return parse(strDate, getDatePattern());
	}

	/**
	 * 使用用户格式提取字符串日期
	 *
	 * @param strDate 日期字符串
	 * @param pattern 日期格式 Date
	 */
	public static Date parse(String strDate, String pattern) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		try {
			return df.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 在日期上增加数个整月
	 *
	 * @param date 日期
	 * @param n    要增加的月数 Date
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加天数
	 *
	 * @param date 日期
	 * @param n    要增加的天数 Date
	 */
	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加小时
	 *
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addHouer(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, n);
		return cal.getTime();
	}

	/**
	 * 获取时间戳 String
	 */
	public static String getTimeString() {
		SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
		Calendar calendar = Calendar.getInstance();
		return df.format(calendar.getTime());
	}

	/**
	 * 获取日期年份
	 *
	 * @param date 日期 String
	 */
	public static String getYear(Date date) {
		return format(date).substring(0, 4);
	}

	/**
	 * 按默认格式的字符串距离今天的天数
	 *
	 * @param date 日期字符串 int
	 */
	public static int countDays(String date) throws ParseException {
		long t = Calendar.getInstance().getTime().getTime();
		Calendar c = Calendar.getInstance();
		c.setTime(parse(date));
		long t1 = c.getTime().getTime();
		return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
	}

	/**
	 * 按用户格式字符串距离今天的天数
	 *
	 * @param date   日期字符串
	 * @param format 日期格式 int
	 */
	public static int countDays(String date, String format) {
		long t = Calendar.getInstance().getTime().getTime();
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(parse(date, format));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long t1 = c.getTime().getTime();
		return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
	}

	/**
	 * 按用户格式字符串距离今天的天数
	 *
	 * @param date   日期字符串
	 * @param format 日期格式 int
	 */
	public static int countSeconds(String date, String format) {
		long t = Calendar.getInstance().getTime().getTime();
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(parse(date, format));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long t1 = c.getTime().getTime();
		return (int) (t / 1000 - t1 / 1000);
	}

	/**
	 * 验证是否是按默认格式的时间格式 boolean
	 */
	public static boolean valid(String str) {
		if (str != null && str.trim().length() > 0) {
			boolean convertSuccess = true;
			SimpleDateFormat format = new SimpleDateFormat(getDatePattern());
			try {
				format.setLenient(false);
				format.parse(str);
			} catch (ParseException e) {
				convertSuccess = false;
			}
			return convertSuccess;
		}
		return false;
	}

	/**
	 * 验证是否是按用户格式的时间格式 boolean
	 */
	public static boolean valid(String str, String format) {
		if (str != null && str.trim().length() > 0) {
			boolean convertSuccess = true;
			SimpleDateFormat df = new SimpleDateFormat(format);
			try {
				df.setLenient(false);
				df.parse(str);
			} catch (ParseException e) {
				convertSuccess = false;
			}
			return convertSuccess;
		}
		return false;
	}

	/**
	 * 获取当前Date date
	 */
	public static Date getCurrentDate() {
		Date today = new Date();
		return today;
	}

	/**
	 * 获取当前Calendar 当前Calendar
	 */
	public static Calendar getCurrentCalendar() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar;
	}

	/**
	 * @param date Calendar
	 */
	public static Calendar getCalendar(Date date) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		return cal;
	}

	public static void main(String[] args) {
//    int seconds = countSeconds("2016-12-20 16:24:58", FORMAT_LONG);
//    System.out.println(seconds + "");
//    Date endDate = DateUtils.getStartOfDay(new Date());
//    Date fromDate = DateUtils.addHouer(endDate, -24);
//    System.out.println("endDate:" +endDate);
//    System.out.println("fromDate:" +fromDate);
		System.out.println(getCurrentDate());
	}

	/**
	 * 根据当前时间得到一个月前的时间 只包括 年月日
	 *
	 * <p>
	 * author: xuyuanyang<br>
	 */
	public static String getDateBefore1M() {
		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int date = now.get(Calendar.DAY_OF_MONTH);
		int[] smallMonths = { 2, 4, 6, 9, 11 };
		if (month - 1 <= 0) { // 跨年
			year = year - 1;
			month = 12;
		}
		if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0) && month == 3) { // 如果1月前是闰年的2月，那么该月的最大天数是28
			month = 2;
			date = date > 28 ? 28 : date;
		} else if (Arrays.asList(smallMonths).contains(month - 1)) {// 如果1月前是小月，那么最大的天数是30
			month = month - 1;
			date = date > 30 ? 30 : date;
		} else {
			month = month - 1;
		}
		String months = String.valueOf(month).length() == 1 ? "0" + month : "" + month;
		String dates = String.valueOf(date).length() == 1 ? "0" + date : "" + date;
		return year + "-" + months + "-" + dates;
	}

	/**
	 * 根据传入的月份获取月初和月末时间 create at 2014年8月25日 上午10:13:57
	 */
	public static String getMaxMonthDate(String date) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

// public static Date getStartOfDay(Date date) {
//    if (date == null) {
//       return null;
//    }
//    DateTime dateTime = new DateTime(date);
//    return dateTime.withTimeAtStartOfDay().toDate();
// }

	public static String dateToString(Date date, String format) {
		String strTemp = new SimpleDateFormat(format).format(date);
		strTemp = strTemp.replace("-", "");
		strTemp = strTemp.replace(".", "");
		strTemp = strTemp.replace(" ", "");
		strTemp = strTemp.replace(":", "");
		return strTemp;
	}

	public static String stringToString(String str) {
		str = str.replace("-", "");
		str = str.replace(".", "");
		str = str.replace(" ", "");
		str = str.replace(":", "");
		str = str.replace("年", "");
		str = str.replace("月", "");
		str = str.replace("日", "");
		return str;
	}

	public static int compareDate(Date date1, Date date2) {
		if (date1.getTime() > date2.getTime()) {
			return 1;
		} else if (date1.getTime() < date2.getTime()) {
			return -1;
		}
		return 0;
	}

	/**
	 * 获取今天00点时间
	 *
	 * @return
	 */
	public static Date getStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * 获取今天23:59点时间
	 *
	 * @return
	 */
	public static Date getnowEndTime() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 获取昨天00点时间
	 *
	 * @return
	 */
	public static Date getyesterDayStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, -1);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * 获取昨天23:59点时间
	 *
	 * @return
	 */
	public static Date getyesterDayEndTime() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, -24);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 获取当前日期本周的第一天，从星期一开始
	 *
	 * @param date
	 * @return
	 */
	public static Date getCurrentWeekDateBegin(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return calendar.getTime();
	}

	/**
	 * 获取上一周的当前时间
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastWeekDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_WEEK, -7);
		return calendar.getTime();

	}

	/**
	 * 获取上一周的开始时间 从星期天开始
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastWeekDateBegin(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.WEEK_OF_MONTH, -1);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return calendar.getTime();
	}

	/**
	 * 获取上一周的开始时间 从星期一开始
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastWeekDateByMonday(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_WEEK, -7);
		calendar.add(Calendar.DATE, 1);// 日期自增
		return calendar.getTime();
	}

	/**
	 * 如果是星期天就获取上周一，否则本周一
	 *
	 * @param date
	 * @return
	 */
	public static Date getCurrentFirstDayOrLastWeekFirstDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		// 如果今天是星期天，就获取上一周
		calendar.setTime(date);
		boolean isFirstSunday = (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
		if (isFirstSunday == true) {
			calendar.add(Calendar.DAY_OF_WEEK, -7);
			calendar.add(Calendar.DATE, 1);// 日期自增
		} else {
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		}
		return calendar.getTime();
	}

	/**
	 * 获取上一周的开始时间 从星期一开始 如果今天是星期天，则去上上周一的开始时间
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastWeekDateFirstDayOrBeforeLastFirstDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		// 如果今天是星期天，就获取上一周
		calendar.setTime(date);
		boolean isFirstSunday = (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
		if (isFirstSunday == true) {
			calendar.add(Calendar.DAY_OF_WEEK, -14);
			calendar.add(Calendar.DATE, 1);// 日期自增
		} else {
			calendar.add(Calendar.DAY_OF_WEEK, -7);
			calendar.add(Calendar.DATE, 1);// 日期自增
		}
		return calendar.getTime();
	}

	/**
	 * 获取本月的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getCurrentMonthDateBegin(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 0);
		return calendar.getTime();
	}

	/**
	 * 获取本月的最后一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getCurrentMonthDateEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, lastDay);
		return calendar.getTime();
	}

	/**
	 * 获取上个月的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastMonthDateBegin(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	/**
	 * 获取上个月的最后一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastMonthDateEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 0);
		calendar.add(Calendar.DATE, -1);
		return calendar.getTime();
	}

	/**
	 * 获取上个月同期日期
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastMonthDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(calendar.MONTH, -1);
		return calendar.getTime();
	}

}
